package sk.stuba.fiit.ltasys.web.mvc.model.service.impl;

import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.stuba.fiit.ltasys.web.mvc.model.dao.CrlAndCertDao;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.CertificateEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.CertificateService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.CommunicatorService;

@Service("certificateService")
public class CertificateServiceImpl  implements CertificateService{

	@Autowired
	CrlAndCertDao crlAndCertDao;
	
	@Autowired
	CommunicatorService communicatorService;

	public void saveCertificate(CertificateEntity certificate) {
		crlAndCertDao.saveCertificate(certificate);
		
	}

	public CertificateEntity getCertificateByIssuerAndSerialNumber(
			String issuer, BigInteger serialNum) {
		return crlAndCertDao.getCertificateByIssuerAndSerialNumber(issuer, serialNum);
	}

	public void deleteCertificate(CertificateEntity certificate) {
		crlAndCertDao.deleteCertificate(certificate);
		
	}
	
	private CertificateEntity getCertificateBySubject(String subject) {
		return crlAndCertDao.getCertificateBySubject(subject);
	}
	
	public CertificateEntity handleCertificate(BigInteger serialNumber) throws Exception {
			CertificateEntity cert = getCertificateByIssuerAndSerialNumber(null, serialNumber);
			if(cert.getCert_id() == null){
				X509Certificate x509Cert = communicatorService.getCertificateFromPKIBySerialNumAndIssuer(""+serialNumber,null);
				cert.convertToCertificateEntity(x509Cert);
				saveCertificate(cert);
			}
			return cert;
	}
	

	public CertificateEntity handleCertificate(String subject) throws Exception{
		CertificateEntity cert = getCertificateBySubject(subject);
		if(cert.getCert_id() == null){
			X509Certificate x509Cert = communicatorService.getCertificateFromPKIBySerialNumAndIssuer(null, subject);
			cert.convertToCertificateEntity(x509Cert);
			saveCertificate(cert);
		}
		return cert;
	}
	
	public CertificateEntity handleCertificate(X509Certificate certificate) throws Exception {
		CertificateEntity cert = getCertificateByIssuerAndSerialNumber(certificate.getIssuerDN().toString(), certificate.getSerialNumber());
		if(cert.getCert_id() == null){
			X509Certificate x509Cert = communicatorService.getCertificateFromPKIBySerialNumAndIssuer(""+certificate.getSerialNumber(), certificate.getIssuerDN().toString());
			cert.convertToCertificateEntity(x509Cert);
			saveCertificate(cert);
		}
		return cert;
	}

	public HashSet<CertificateEntity> handleCertificate(HashSet<BigInteger> certificates) throws Exception {
		HashSet<CertificateEntity> result = new HashSet<CertificateEntity>();
		for (BigInteger serialNum : certificates) {
			CertificateEntity cert = getCertificateByIssuerAndSerialNumber(null, serialNum);
			if(cert.getCert_id() == null){
				X509Certificate x509Cert = communicatorService.getCertificateFromPKIBySerialNumAndIssuer(""+serialNum,null);
				cert.convertToCertificateEntity(x509Cert);
				saveCertificate(cert);
				result.add(cert);
			}else{
				result.add(cert);
			}	
		}
		return result;
	}
}
